package com.example.demo.api.controller;

import com.example.demo.domain.entity.User;
import com.example.demo.api.dto.UserDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

//@Slf4j
@RestController
public class UserController {
    private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class);

    /**
     * 获取id*10的数据
     * @param id
     * @return
     */
    @GetMapping("/train1")
    public ResponseEntity<Integer> train1(@RequestParam Integer id){
        return ResponseEntity.ok(id*10);
    }

    /**
     * 获取用户信息
     * @param user
     * @return
     */
    @PostMapping("/user")
    public  ResponseEntity<?> user(@RequestBody User user){
        return ResponseEntity.ok(user.toString());
    }

    @PutMapping("/user")
    public UserDTO userPut(@RequestBody User user){
        UserDTO userDTO = new UserDTO();
        if(user.getAge() >= 18){
            BeanUtils.copyProperties(user,userDTO);
            LOGGER.info("对象转换");
            userDTO.setMark("已成年");
        }else {
            BeanUtils.copyProperties(user,userDTO);
            userDTO.setMark("未成年");
        }
        return userDTO;
    }

    @DeleteMapping("/user")
    public String userDel(@RequestBody User user){
        LOGGER.info("删除ing");
        return user.getName()+"信息已删除";
    }
}
